package com.ruoyi.zsix.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.zsix.domain.process.ProcessManage;
import com.ruoyi.zsix.domain.taskAssignee.FlowElementAttrs;
import com.ruoyi.zsix.domain.taskAssignee.GenerWork;
import com.ruoyi.zsix.domain.taskAssignee.OvTaskEntity;
import com.ruoyi.zsix.mapper.taskAssigneeMapper.FlowElementAttrsMapper;
import com.ruoyi.zsix.mapper.taskAssigneeMapper.GenerWorkMapper;
import com.ruoyi.zsix.mapper.taskAssigneeMapper.OvTaskEntityMapper;
import com.ruoyi.zsix.service.IProcessManageService;
import com.ruoyi.zsix.service.manager.IProcessMManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class IProcessManageServiceImpl implements IProcessManageService {
    @Autowired
    private IProcessMManager processMManager;
    @Autowired
    private GenerWorkMapper generWorkMapper;
    @Autowired
    private FlowElementAttrsMapper flowElementAttrsMapper;
    @Autowired //自定义
    private OvTaskEntityMapper ovTaskEntityMapper;
    /**
     * 获取当前系统登陆人发起的流程列表
     * @param params
     * @return
     */
    @Override
    public HashMap<String,Object> getMyStartListPage(JSONObject params){
        String title=params.getString("title");
        String flowName=params.getString("flowName");
        Integer page=params.getInteger("page");
        Integer pagesize=params.getInteger("pageSize");
        String state=params.getString("state");
        return processMManager.getMyStartList(title,flowName,state,page,pagesize);
    }

    /**
     * 获取我审核的数据
     * @param params
     * @return
     */
    @Override
    public HashMap<String,Object> getMyProcessPage(JSONObject params){
        String title=params.getString("title");
        String flowName=params.getString("flowName");
        Integer page=params.getInteger("page");
        Integer pagesize=params.getInteger("pageSize");
        String state=params.getString("state");
        return processMManager.getMyProcessPage(title,flowName,state,page,pagesize);
    }
    /**
     * 获取流程实例补充参数
     * @param processManage
     * @return
     */
    @Override
    public HashMap<String,Object> getManageParams(ProcessManage processManage){
        HashMap<String,Object> hashMap=new HashMap<>();
        List<OvTaskEntity> tasks = ovTaskEntityMapper.ovTaskEntityByProcessInsId(processManage.getProc_inst_id_());
        GenerWork generWork=generWorkMapper.getGenerWorkByInst(processManage.getProc_inst_id_());
        if(generWork==null)
            generWork=generWorkMapper.generWork(processManage.getBusiness_key_());
        if(tasks.size()>0) {
            OvTaskEntity task = tasks.get(0);
            FlowElementAttrs flowElementAttrs=
                    flowElementAttrsMapper.getFlowElementAttrs(task.getTask_def_key_());
            hashMap.put("form_type",flowElementAttrs.getForm_type());
            hashMap.put("form_url",flowElementAttrs.getForm_url());
            hashMap.put("taskid",task.getId_());
            hashMap.put("task_def_name",task.getName_());
        }
        else{
            try{
                hashMap.put("form_type","0");
                hashMap.put("form_url","");
            }
            catch (Exception ex){
                hashMap.put("form_type","1");
                hashMap.put("form_url",generWork.getMap_json());
            }
            hashMap.put("taskid",'0');
            hashMap.put("task_def_name","结束");
        }
        hashMap.put("map_json",generWork.getMap_json());
        hashMap.put("data_json",generWork.getData_json());
        return hashMap;
    }
}
